/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.io.IOException;
import java.util.logging.Filter;
import java.util.logging.LogRecord;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Professor;

/**
 *
 * @author Wagner
 */
public class UserLoginFilter implements Filter{
    
    private ServletContext context = null;  
  
    public void init(FilterConfig config) throws ServletException {  
        System.out.println("Iniciando Filtro!");
        this.context = config.getServletContext();  
    }  
    
    public synchronized void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)   
        throws IOException, ServletException {  
  
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        Professor userManager = (Professor) req.getSession().getAttribute("Usuario");

        if (userManager == null) {
            res.sendRedirect(req.getContextPath() + "/_frmCadProfessor.xhtml");
        } else {
            chain.doFilter(request, response);
        }

    } 
    
    @Override
    public boolean isLoggable(LogRecord record) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
